package org.example.easy;

import java.util.HashMap;

/**
 * @author yanyun zhu
 * @version 1.0
 * @date 2021/11/16 14:14
 * 题号  1
 */

public class AddSum {

    // 时间复杂度  O(n²)   空间复杂度O(1)
    public static int[] twoSum(int[] nums, int target) {
        int length = nums.length;
        for(int i =0 ; i < length; i++){
            for(int j = i +1 ; j < length; j++){
                if(nums[i] + nums[j] == target){
                    return new int[]{i, j};
                }
            }
        }
        return new int[0];
    }

    //
    public static int[] twoSum2(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i <nums.length; i++ ){
            if(map.containsKey(target - nums[i])){
                return new int[]{map.get(target - nums[i]), i};
            }
            map.put(nums[i], i);
        }
        return new int[0];
    }

    public static void main(String[] args) {
        int[] ints = twoSum2(new int[]{-1, -2, -3,-4, -5}, -8);
        System.out.println(ints[0]);
        System.out.println(ints[1]);
    }

}
